package com.zeny.edu.eduservice.service.impl;

import com.zeny.edu.eduservice.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName RedisServiceImpl
 * @Description TODO
 * @Author zeny
 * @Date 2020/3/4 0004 0:32
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @param
     * @return java.lang.String
     * @Description 生成提交码
     * @Date 2020/3/4 0004 0:33
     **/
    @Override
    public String generatePostCode() {
        String code = UUID.randomUUID().toString() + new Date();
        redisTemplate.opsForValue().set(code, "hello", 30, TimeUnit.SECONDS);
        return code;
    }

    /**
     * @param code
     * @return boolean
     * @Description 验证提交码并删除
     * @Date 2020/3/4 0004 0:34
     **/
    @Override
    public boolean checkCode(String code) {
//      String value = (String) redisTemplate.opsForValue().getAndSet(code, "");
        Boolean delete = redisTemplate.delete(code);
        return delete;
    }
}
